package com.ellfors.common.base.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter4.BaseQuickAdapter

/**
 * 只有一种类型的Adapter
 */
abstract class BaseSingleAdapter<T : Any, VB : ViewBinding> :
    BaseQuickAdapter<T, BaseSingleAdapter.SingleViewHolder<VB>>() {

    class SingleViewHolder<VB : ViewBinding>(val binding: VB) : ViewHolder(binding.root)

    override fun onCreateViewHolder(
        context: Context, parent: ViewGroup, viewType: Int
    ): SingleViewHolder<VB> {
        return SingleViewHolder(
            initViewBinding(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: SingleViewHolder<VB>, position: Int, item: T?) {
        convert(holder, position, item)
    }

    override fun isFullSpanItem(itemType: Int): Boolean {
        return true
    }

    /**
     * example: ItemTestBinding::inflate
     */
    abstract val initViewBinding: (LayoutInflater, ViewGroup, Boolean) -> VB

    /**
     * 渲染ViewHolder
     */
    abstract fun convert(holder: SingleViewHolder<VB>, position: Int, item: T?)
}